package com.stock.demo.function.home.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.sliderlibrary.SliderLayout;
import com.example.sliderlibrary.SliderTypes.BaseSliderView;
import com.example.sliderlibrary.SliderTypes.TextSliderView;
import com.quinny898.library.persistentsearch.SearchBox;
import com.quinny898.library.persistentsearch.SearchResult;
import com.stock.demo.MainActivity;
import com.stock.demo.R;
import com.stock.demo.adapter.HomeFollowBuyAdapter;
import com.stock.demo.adapter.HomeLiveBroadcastAdapter;
import com.stock.demo.framework.ui.fragment.BaseNetFragment;
import com.stock.demo.framework.ui.view.MeasuredRecyclerView;
import com.stock.demo.function.home.presenter.HomePresenter;
import com.stock.demo.function.home.presenter.HomePresenterImpl;
import com.stock.demo.model.HomeBannerBean;
import com.stock.demo.model.HomeFollowBuyBean;
import com.stock.demo.model.HomeLiveBroadcastBean;
import com.stock.demo.util.Urls;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XuZhaoyu on 2017/6/1.
 */

public class HomeFragment extends BaseNetFragment implements HomeView {

    private View mView;
    private SliderLayout mSlBanner;
    private HomePresenter mPresenter;
    private SearchBox mSearchBox;
    private ImageView mIvUserIcon;
    private ImageView mIvMessage;
    private LinearLayout mLlCourseLearning;
    private LinearLayout mLlStockSimulator;
    private LinearLayout mLlStockGame;
    private LinearLayout mLlStockLive;
    private LinearLayout mLlStockHot;
    private MeasuredRecyclerView mRvFollowBuy;
    private MeasuredRecyclerView mRvLiveBroadcast;
    private MeasuredRecyclerView mRvSelectedNews;

    @Override
    public View onCreateSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_home, container, false);
        return mView;
    }

    @Override
    public void onReload(View v) {
        mPresenter.getBanner();
    }

    @Override
    public void initView() {
        mSlBanner = (SliderLayout) mView.findViewById(R.id.slider_layout_home_banner);
        mSearchBox = (SearchBox) mView.findViewById(R.id.search_box_home);
        mIvUserIcon = (ImageView) mView.findViewById(R.id.iv_home_user_icon);
        mIvMessage = (ImageView) mView.findViewById(R.id.iv_home_message);
        mLlCourseLearning = (LinearLayout) mView.findViewById(R.id.ll_home_course_learning);
        mLlStockSimulator = (LinearLayout) mView.findViewById(R.id.ll_home_stock_simulator);
        mLlStockGame = (LinearLayout) mView.findViewById(R.id.ll_home_stock_game);
        mLlStockLive = (LinearLayout) mView.findViewById(R.id.ll_home_live_broadcast);
        mLlStockHot = (LinearLayout) mView.findViewById(R.id.ll_home_stock_hot);
        mRvFollowBuy = (MeasuredRecyclerView) mView.findViewById(R.id.rv_home_follow_buy);
        mRvLiveBroadcast = (MeasuredRecyclerView) mView.findViewById(R.id.rv_home_live_broadcast);
        mRvSelectedNews = (MeasuredRecyclerView) mView.findViewById(R.id.rv_home_selected_news);
        mIvUserIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFunctions().invokeFun(MainActivity.FUN_SLIDING_MENU_TOGGLE);
            }
        });
        mIvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageActivity.startAction(getContext());
            }
        });
        mLlCourseLearning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CourseLearningListActivity.startAction(getContext());
            }
        });
        mLlStockSimulator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockSimulatorActivity.startAction(getContext());
            }
        });
        mLlStockGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockGameActivity.startAction(getContext());
            }
        });
        mLlStockLive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveBroadcastListActivity.startAction(getContext());
            }
        });
        mLlStockHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockHotActivity.startAction(getContext());
            }
        });
        ArrayList<HomeFollowBuyBean> homeFollowBuyBeen = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            homeFollowBuyBeen.add(new HomeFollowBuyBean());
        }
        HomeFollowBuyAdapter homeFollowBuyAdapter = new HomeFollowBuyAdapter(getContext(), homeFollowBuyBeen);
        mRvFollowBuy.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvFollowBuy.setAdapter(homeFollowBuyAdapter);
        ArrayList<HomeLiveBroadcastBean> homeLiveBroadcastBean = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            homeLiveBroadcastBean.add(new HomeLiveBroadcastBean());
        }
        HomeLiveBroadcastAdapter homeLiveBroadcastAdapter = new HomeLiveBroadcastAdapter(getContext(), homeLiveBroadcastBean);
        mRvLiveBroadcast.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvLiveBroadcast.setAdapter(homeLiveBroadcastAdapter);
        mRvSelectedNews.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvSelectedNews.setAdapter(homeLiveBroadcastAdapter);
        initSearchBox();
    }

    @Override
    public void initData() {
        mPresenter = new HomePresenterImpl(this);
        mPresenter.getBanner();
    }

    private void initSearchBox() {
        mSearchBox.setHint("搜股票");
        mSearchBox.setLogoText("搜股票");
        mSearchBox.setMaxLength(20);
        mSearchBox.setMenuListener(new SearchBox.MenuListener() {

            @Override
            public void onMenuClick() {
                Toast.makeText(getContext(), "点击了菜单按键", Toast.LENGTH_SHORT).show();
            }

        });
        mSearchBox.setSearchListener(new SearchBox.SearchListener() {

            @Override
            public void onSearchOpened() {

            }

            @Override
            public void onSearchClosed() {

            }

            @Override
            public void onSearchTermChanged(String term) {

            }

            @Override
            public void onSearch(String searchTerm) {
                mSearchBox.addSearchable(new SearchResult(searchTerm, getResources().getDrawable(R.mipmap.search_history_icon)));
                Toast.makeText(getContext(), searchTerm + " Searched", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResultClick(SearchResult result) {

            }

            @Override
            public void onSearchCleared() {

            }

        });
    }

    @Override
    public void onGetBanner(List<HomeBannerBean.EntityBean.IndexCenterBannerBean> list) {
        mSlBanner.removeAllSliders();
        for (int i = 0; i < list.size(); i++) {
            TextSliderView textSliderView = new TextSliderView(getContext());
            String path = Urls.STATIC_BANNER_BASE_URL + list.get(i).getImagesUrl();
            textSliderView.image(path).setScaleType(BaseSliderView.ScaleType.Fit);
            mSlBanner.addSlider(textSliderView);
        }
        mSlBanner.setPresetTransformer(SliderLayout.Transformer.ZoomOutSlide);
        mSlBanner.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mSlBanner.setDuration(3000);
    }

}
